package com.password.desktop.page;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import org.apache.commons.lang3.StringUtils;

import com.password.desktop.UI.AbstractMovedWindow;
import com.password.desktop.UI.InputForm;
import com.password.desktop.UI.PMButton;
import com.password.desktop.service.LoginService;
import com.password.desktop.util.MessageUtil;
import com.password.desktop.util.SysUtil;
import com.password.entity.User;

public class RegisterWindow extends AbstractMovedWindow {

	private static final long serialVersionUID = 1L;
	LoginService loginService = LoginService.builderLoginService();
	private static final int HEIGHT_WINDOW = 300; // 窗口高度
	private static final int WIDTH_WINDOW = 348; // 窗口宽度
	private static final int HEIGHT_HEAD = 32;

	private JPanel head; // 头部容器
	JLabel logo, minW, closeW;// logo,最小化，关闭
	InputForm userName, password, rePassword;

	public static void main(String[] args) {
		new RegisterWindow();
	}

	public RegisterWindow() {
		getContentPane().add(getContent());
		getContentPane().add(getHead());

		initSet();
	}

	public void initSet() {
		this.setSize(WIDTH_WINDOW, HEIGHT_WINDOW);
		super.initSet();
		setTitle("注册密码管理器账户");
		setBackground(Color.white);
		setVisible(true);
	}

	public JPanel getContent() {
		JPanel content = new JPanel();
		content.setLayout(null);
		content.setBackground(Color.white);
		content.setOpaque(true);

		userName = new InputForm("用户名：");
		userName.setBounds(0, 16, 320, 48);
		password = new InputForm("密码：", JPasswordField.class);
		password.setBounds(0, 70, 320, 48);
		rePassword = new InputForm("重复密码：", JPasswordField.class);
		rePassword.setBounds(0, 124, 320, 48);

		JLabel reg = new PMButton("注册", PMButton.STYLE_PRIMARY, (e) -> {
			String text = userName.getText();
			String text2 = password.getText();
			String text3 = rePassword.getText();

			if (StringUtils.isBlank(text)) {
				MessageUtil.showMessage(RegisterWindow.this, "用户名不能为空");
				return;
			}
			if (StringUtils.isBlank(text2)) {
				MessageUtil.showMessage(RegisterWindow.this, "密码不能为空");
				return;
			}
			if (!StringUtils.equals(text2, text3)) {
				MessageUtil.showMessage(RegisterWindow.this, "两次输入的密码不一致");
				return;
			}
			try {
				loginService.registerUser(text, text2);
				dispose();
				new LoginWindow();
				return;
			} catch (Exception e2) {
				MessageUtil.showMessage(RegisterWindow.this, e2.getMessage());
			}

		});
		reg.setBounds(76, 198, 80, 32);

		JLabel cancle = new PMButton("取消", PMButton.STYLE_INFO, (e) -> {
			dispose();
			new LoginWindow();
		});
		cancle.setBounds(206, 198, 80, 32);

		content.add(userName);
		content.add(password);
		content.add(rePassword);
		content.add(reg);
		content.add(cancle);

		content.setBounds(0, HEIGHT_HEAD + 1, WIDTH_WINDOW, HEIGHT_WINDOW - HEIGHT_HEAD - 1);
		return content;
	}

	public JPanel getHead() {
		head = new JPanel();
		head.setBackground(new Color(153, 255, 204));
		head.setLayout(null);
		head.setVisible(true);
		head.setBounds(0, 0, WIDTH_WINDOW, HEIGHT_HEAD);
		head.setForeground(Color.WHITE);

		JLabel backgroud = new JLabel(new ImageIcon(getResoure("素材/1.gif")));
		backgroud.setOpaque(false);
		JLabel title = new JLabel("PM 注册密码管理器账户");
		logo = new JLabel(SysUtil.ICON_LOGO_MIDDLE);
		minW = new JLabel(SysUtil.ICON_MIN);
		closeW = new JLabel(SysUtil.ICON_CLOSE);
		backgroud.setBounds(0, 0, WIDTH_WINDOW, HEIGHT_HEAD);
		logo.setBounds(0, 0, HEIGHT_HEAD, HEIGHT_HEAD);
		title.setBounds(HEIGHT_HEAD + 2, 0, WIDTH_WINDOW - 64, HEIGHT_HEAD);
		title.setFont(SysUtil.FONT_DEFAULT_MIN);
		minW.setBounds(WIDTH_WINDOW - 64 - 4, 0, HEIGHT_HEAD, HEIGHT_HEAD);
		minW.setBackground(new Color(0, 0, 0, 0.3f));
		closeW.setBounds(WIDTH_WINDOW - 32, 0, HEIGHT_HEAD, HEIGHT_HEAD);
		closeW.setBackground(new Color(0, 0, 0, 0.3f));
		// 事件区域
		minW.addMouseListener(this);
		closeW.addMouseListener(this);

		head.add(logo);
		head.add(title);
		head.add(minW);
		head.add(closeW);
		head.add(backgroud);

		return head;
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.getSource() == minW) {
			setExtendedState(JFrame.ICONIFIED);
		} else if (e.getSource() == closeW) {
			System.exit(0);
		} else if (e.getSource() == this) {
			super.mousePressed(e);
		}

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (e.getSource() == minW) {
			minW.setOpaque(true);
			minW.setBackground(Color.lightGray);
		} else if (e.getSource() == closeW) {
			closeW.setOpaque(true);
			closeW.setBackground(Color.lightGray);
		}

	}

	@Override
	public void mouseExited(MouseEvent e) {
		if (e.getSource() == minW) {
			minW.setOpaque(false);
			minW.setBackground(new Color(0, 0, 0, 0.3f));
		} else if (e.getSource() == closeW) {
			closeW.setOpaque(false);
			closeW.setBackground(new Color(0, 0, 0, 0.3f));
		}
	}

}
